package jp.syuriken.snsw.terenaibot.format.function;

import jp.syuriken.snsw.terenaibot.format.CompileArg;
import jp.syuriken.snsw.terenaibot.format.FormatSyntaxException;
import jp.syuriken.snsw.terenaibot.format.PrecompiledSyntaxGroup;

/**
 * TODO snsoftware
 * 
 * @author $Author: snswinhaiku.lo@gmail.com $ */
public class IntAddFunction extends AbstractFormatFunction {
	
	@Override
	public String getReturnValue(PrecompiledSyntaxGroup[] args, CompileArg compileArg) throws FormatSyntaxException {
		checkArgLength(args, compileArg, "+", 2, -1);
		
		int sum = 0;
		for (int i = 0; i < args.length; i++) {
			try {
				sum += Integer.parseInt(args[i].format(compileArg));
			} catch (NumberFormatException e) {
				throw new FormatSyntaxException(String.format(
						"Function<+>:arg%d is not integer value: %s (formatted:%s)", i, args[i].toString(),
						args[i].format(compileArg)));
			}
		}
		return String.valueOf(sum);
	}
}
